<?php
class koudai_order_thshopControl extends skymvc{
	public $k="koudai";
	public function __construct(){
		parent::__construct();
	}
	
	public function onDefault(){
		$url=APPKOUDAI."?m=koudai_order_thshop";
		$where=" k='".$this->k."' AND shopid=".SHOPID." ";
		 
		$start=get('per_page','i');
		$limit=20;
		$option=array(
			"where"=>$where,
			"order"=>" id DESC",
			"start"=>$start,
			"limit"=>$limit
		);
		$rscount=true;
		$data=M("koudai_order_thshop")->select($option,$rscount);
		if($data){
			foreach($data as $k=>$v){
				$v['order']=M("koudai_order")->selectRow("order_id=".$v['order_id']);
				$v['products']=$this->orderproduct($v['order_id']);
				$data[$k]=$v;
			}
			
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$this->smarty->assign(array(
			"data"=>$data,
			"pagelist"=>$pagelist
		));
		$this->smarty->display("koudai_order_thshop/index.html");
	}
	
	public function onReceived(){
		
		$id=get_post('id','i');
		$row=M("koudai_order_thshop")->selectRow("id=".$id);
		if($row['status']==1 or $row['isreceived']==2){
			$this->goAll("该订单已处理了");
		}
		M("koudai_order_thshop")->begin();
		M("koudai_order_thshop")->update(array(
			"status"=>1,
			"isreceived"=>2
		),"id=".$id);
		
		M("koudai_order_thshop")->commit();
		$this->goALl("接收成功");
	}
	
	public function onCancel(){
		$id=get_post('id','i');
		M("koudai_order_thshop")->update(array(
			"status"=>2
		),"id=".$id);
		$this->goALl("取消成功");
	}
	
	public function orderproduct($order_id){
		
		$order_product=M("koudai_order_product")->select(array("where"=>"order_id=".intval($order_id)));
		if($order_product){
			foreach($order_product as $k=>$v){
				$productids[]=$v['productid'];
			}
			$products=M("product")->getListByIds($productids);
			foreach($order_product as $k=>$v){
				$p=$products[$v['productid']];
				$p['order_price']=$v['price'];
				$p['amount']=$v['amount'];
				$p['total_money']=$v['amount']*$v['price'];
				$p['gznum']=$v['gznum'];
				$p['thnum']=$v['thnum'];
				$p['jsnum']=$v['jsnum'];
				$p['order_product_id']=$v['id'];
				$order_product[$k]=$p;
			}
		}
		return $order_product;	
	}
	
}

?>